package com.imoocdl.controller;

import com.imoocdl.config.ProjectUrl;
import com.imoocdl.constanat.CookieConstant;
import com.imoocdl.constanat.RedisConstant;
import com.imoocdl.dataobject.SellerInfo;
import com.imoocdl.enums.ResultEnum;
import com.imoocdl.service.SellerService;
import com.imoocdl.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by pc on 2018/7/27.
 */
@Controller
@RequestMapping("/seller")
@Slf4j
public class SellerUserController {
    @Autowired
    private SellerService sellerService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ProjectUrl projectUrl;
    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid, HttpServletResponse response, Map<String, Object> map){
        //去数据库 匹配openid用户是否存在
        SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid(openid);
        if(sellerInfo == null){
            //返回到错误页面
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        //存token到redis
        String token = UUID.randomUUID().toString();
        Integer expire = RedisConstant.EXPIRE;
        stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS);
        //存token到cookie
        CookieUtil.set(response,"token", CookieConstant.TOKEN,CookieConstant.EXPIRE);
        return new ModelAndView("redirect:"+projectUrl.getSell()+"/sell/seller/order/list");
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,HttpServletResponse response,Map<String,Object> map){
    //1 从cookie 查询token
       Cookie cookie =  CookieUtil.get(request,CookieConstant.TOKEN);
       if(cookie != null){
           //2清楚redis
           stringRedisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
           //3清除cookie
           CookieUtil.set(response,CookieConstant.TOKEN,null,0);
           map.put("msg",ResultEnum.LOGOUT_SUCCESS.getMessage());
           map.put("url","/sell/seller/order/list");
           return new ModelAndView("common/success",map);
       }
        map.put("msg",ResultEnum.LOGOUT_FAIL.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/error",map);
    }
}
